package cn.wolfcode.luowowo.mgrsite.job;

import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.vo.StrategyDetailVO;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.util.BeanUtil;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 将我们的redis数据持久化到SQL中
 */
@Component
public class RedisDataPersistenceJob {

    @Reference
    private IStrategyStatisVOService strategyStatisVOService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    //@Scheduled(cron= "0/10 * * * * ?")
    public void redisDataPersistence() {
        System.out.println("-------------------begin---------------------");
        String patten = RedisKeys.NUM_TYPE_VO.getPrefix() + "*";
        List<StrategyStatisVO> list = strategyStatisVOService.getStratesVO(patten);
        //这里我们使用自定义的vo(只存储了阅读数,点赞数等...)
        StrategyDetailVO detailVO = new StrategyDetailVO();
        if (list != null && list.size() > 0) {
            for (StrategyStatisVO vo : list) {
                BeanUtil.copyProperties(detailVO, vo);
                strategyDetailService.updateByVO(detailVO);
            }
        }
        System.out.println("-------------------end--------------------");
    }

}
